package com.rabincorp.facepoll.domain.entity.poll;

import com.rabincorp.facepoll.application.FacepollConstant;
import com.rabincorp.facepoll.application.FacepollConstant.PollType;
import com.rabincorp.facepoll.domain.entity.BasicEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

/**
 *
 * @author isakrabin
 */
@Entity
public class Poll extends BasicEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long id;
    
    private String title;
    
    @Column(nullable = false)
    private String question;
    
    @OneToMany(cascade= CascadeType.ALL)
    List<PollOption> options = new ArrayList<PollOption>();
    
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date startDate;
    
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date endDate;
    
    private Boolean active = Boolean.TRUE;
    
    @Enumerated(EnumType.ORDINAL)
    private FacepollConstant.PollType type = FacepollConstant.PollType.PUBLIC;
    
    private Long totalVote = 0L;
    
    @Column(nullable=false)
    private Long ownerId;

    public Poll() {
    }

    public Poll(String title, String question, List<String> options, Long ownerId) {
        this.title = title;
        this.question = question;
        for (String option : options) {
            PollOption answer = new PollOption(option, null, null);
            this.options.add(answer);
        }
        this.ownerId=ownerId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void addOptions(String description, Integer value, String image) {
        PollOption option = new PollOption(description, value, image);
        options.add(option);
    }

    public List<PollOption> getOptions() {
        return options;
    }

    public void setOptions(List<PollOption> options) {
        this.options = options;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public FacepollConstant.PollType getType() {
        return type;
    }

    public void setType(PollType type) {
        this.type = type;
    }

    public Long getTotalVote() {
        return totalVote;
    }

    public void setTotalVote(Long totalVote) {
        this.totalVote = totalVote;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }
}
